package com.woniuxy.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

/**
 * @Author: 马宇航
 * @Todo: TODO
 * @DateTime: 25/09/11/星期四 10:57
 * @Component: 成都蜗牛学苑
 **/
@RestController
@RequestMapping("/ai")
@CrossOrigin
public class AIFileUploadController {
    @Value("${file.upload.path}")
    private String uploadPath;

    @PostMapping("/upload")
    public ResponseEntity<Map> upload(@RequestParam("file") MultipartFile file) throws IOException {
        //1.先判断有没有这个静态资源路径
        if (Files.notExists(Paths.get(uploadPath))) {
            //2.如果没有文件夹就创建 递归创建
            Files.createDirectories(Paths.get(uploadPath));
        }
        //3.如果有文件夹就直接上传
        file.transferTo(Paths.get(uploadPath, file.getOriginalFilename()));
        //4.返回给前端 文件路径和名字，前端要访问一下,后期minio的链接
        return new ResponseEntity(Map.of("filePath", "/public/" + file.getOriginalFilename()), HttpStatusCode.valueOf(200));
    }
}